// if the browser support SSE
if ('EventSource' in window) {
    // init SSE
    let url = "/push/sse";
    let source = new EventSource(url);
    let bd = document.querySelector('body');
    let btn = document.querySelector('#btnCls');
    source.onopen = (event) => {
        console.log("开启SSE");
    };

    source.onmessage = (event) => {
        let data = event.data;
        bd.appendChild(document.createTextNode(data));
        btn.innerHTML = data;
    };

    // Listen like event
    source.addEventListener('evt', function (event) {
        let data = event.data;
        bd.appendChild(document.createTextNode(data));
        bd.scrollIntoView(false);
        btn.innerHTML = data;
    }, false);

    // Called when error occur.
    source.onerror = (event) =>{
        console.log("error occur.");
        console.log(event);
    };
    btn.addEventListener('click',
            function (){
                source.close();
            },
            false)
}